<?php

namespace app\admin\model\yihu\nursing;

use app\admin\model\yihu\CompanyModel;
use app\admin\model\yihu\ElderlyModel;
use think\Model;


class Task extends Model
{
    protected $connection = 'database.mzshow';
    protected $table = 'yihu_nursing_task';  // 表名

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = false;

    // 定义时间戳字段名
    protected $createTime = false;
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'drug_period_time_text',
        'finish_time_text',
        'task_status_time_text',
        'task_start_time_text'
    ];

    public function getDrugPeriodTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['drug_period_time']) ? $data['drug_period_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getFinishTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['finish_time']) ? $data['finish_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getTaskStatusTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['task_status_time']) ? $data['task_status_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getTaskStartTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['task_start_time']) ? $data['task_start_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setDrugPeriodTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setFinishTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setTaskStatusTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setTaskStartTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }
    /**
     * 关联公司
     */
    public function authGroupCompany(){
        return $this->belongsTo(CompanyModel::class,'company_id','id',[],'left')->field('title as company_name');
    }

    /**
     * 关联老人
     */
    public function yihuElderly(){
        return $this->belongsTo(ElderlyModel::class,'elderly_id','id',[],'left')->field('nickname as elderly_name');
    }

}
